package com.letfox.common.datasource.druid;

import java.sql.SQLException;

import org.springframework.beans.factory.InitializingBean;

import com.alibaba.druid.pool.DruidDataSource;


/**
 * 继承DruidDataSource
 */
public class MybatisDruidDataSource extends DruidDataSource implements InitializingBean {

    private static final long serialVersionUID = -6617169434574706904L;

    private DruidConfig druidConfig;

    @Override
    public void afterPropertiesSet() throws Exception {
        config();
        init();
    }

    /**
     * 对于非基本数据类型的字段，只有为null时才覆盖
     * 
     * @throws SQLException
     */
    private void config() throws SQLException {
        if (null == druidConfig) {
            return;
        }
        if (null != druidConfig.getDriverClassName() && null == getDriverClassName()) {
            setDriverClassName(druidConfig.getDriverClassName());
        }
        if (null != druidConfig.getUsername() && null == getUsername()) {
            setUsername(druidConfig.getUsername());
        }
        if (null != druidConfig.getPassword() && null == getPassword()) {
            setPassword(druidConfig.getPassword());
        }
        if (null != druidConfig.getDefaultAutoCommit()) {
            setDefaultAutoCommit(druidConfig.getDefaultAutoCommit());
        }
        if (null != druidConfig.getValidationQuery() && null == getValidationQuery()) {
            setValidationQuery(druidConfig.getValidationQuery());
        }
        if (null != druidConfig.getInitialSize()) {
            setInitialSize(druidConfig.getInitialSize());
        }
        if (null != druidConfig.getMinIdle()) {
            setMinIdle(druidConfig.getMinIdle());
        }
        if (null != druidConfig.getTimeBetweenLogStatsMillis()) {
            setTimeBetweenLogStatsMillis(druidConfig.getTimeBetweenLogStatsMillis());
        }
        if (null != druidConfig.getMaxActive()) {
            setMaxActive(druidConfig.getMaxActive());
        }
        if (null != druidConfig.getMaxWait()) {
            setMaxWait(druidConfig.getMaxWait());
        }
        if (null != druidConfig.getDefaultTransactionIsolation() && null == getDefaultTransactionIsolation()) {
            setDefaultTransactionIsolation(druidConfig.getDefaultTransactionIsolation());
        }
        if (null != druidConfig.getTestOnBorrow()) {
            setTestOnBorrow(druidConfig.getTestOnBorrow());
        }
        if (null != druidConfig.getTestWhileIdle()) {
            setTestWhileIdle(druidConfig.getTestWhileIdle());
        }
        if (null != druidConfig.getTestOnReturn()) {
            setTestOnReturn(druidConfig.getTestOnReturn());
        }
        if (null != druidConfig.getTimeBetweenEvictionRunsMillis()) {
            setTimeBetweenEvictionRunsMillis(druidConfig.getTimeBetweenEvictionRunsMillis());
        }
        if (null != druidConfig.getMinEvictableIdleTimeMillis()) {
            setMinEvictableIdleTimeMillis(druidConfig.getMinEvictableIdleTimeMillis());
        }
        if (null != druidConfig.getRemoveAbandoned()) {
            setRemoveAbandoned(druidConfig.getRemoveAbandoned());
        }
        if (null != druidConfig.getRemoveAbandonedTimeout()) {
            setRemoveAbandonedTimeout(druidConfig.getRemoveAbandonedTimeout());
        }
        if (null != druidConfig.getLogAbandoned()) {
            setLogAbandoned(druidConfig.getLogAbandoned());
        }
        if (null != druidConfig.getPasswordCallback() && null == getPasswordCallback()) {
            setPasswordCallback(druidConfig.getPasswordCallback());
        }
        if (null != druidConfig.getValidConnectionChecker() && null == getValidConnectionChecker()) {
            setValidConnectionChecker(druidConfig.getValidConnectionChecker());
        }
        if (null != druidConfig.getFilters()) {
            setFilters(druidConfig.getFilters());
        }
        if (null != druidConfig.getExceptionSorter() && null == getExceptionSorter()) {
            setExceptionSorter(druidConfig.getExceptionSorter());
        }
        if (null != druidConfig.getConnectProperties()) {
            setConnectProperties(druidConfig.getConnectProperties());
        }
    }

    public void setDruidConfig(DruidConfig druidConfig) {
        this.druidConfig = druidConfig;
    }
}